package com.mdd.maduoduo.presenter;


import static androidx.core.content.ContextCompat.startActivity;

import android.content.Context;
import android.content.Intent;

import com.mdd.maduoduo.activity.MaxActivity;
import com.mdd.maduoduo.di.PerActivity;
import com.mdd.maduoduo.domain.interactor.AddMsgData;
import com.mdd.maduoduo.domain.interactor.AddShareCodeData;
import com.mdd.maduoduo.domain.interactor.BaseObserver;
import com.mdd.maduoduo.domain.interactor.CheckVersionData;
import com.mdd.maduoduo.domain.interactor.DeleteCodeData;
import com.mdd.maduoduo.domain.interactor.DeleteShareCodeData;
import com.mdd.maduoduo.domain.interactor.ExchangeData;
import com.mdd.maduoduo.domain.interactor.GetBannerListData;
import com.mdd.maduoduo.domain.interactor.GetMaxData;
import com.mdd.maduoduo.domain.interactor.GetShareCodeData;
import com.mdd.maduoduo.domain.interactor.GetStationData;
import com.mdd.maduoduo.domain.interactor.GetUserData;
import com.mdd.maduoduo.domain.interactor.LoginData;
import com.mdd.maduoduo.domain.interactor.LogoutData;
import com.mdd.maduoduo.domain.interactor.MaxConfigData;
import com.mdd.maduoduo.domain.interactor.QueryListData;
import com.mdd.maduoduo.domain.interactor.SetSwitchData;
import com.mdd.maduoduo.domain.interactor.UpdateCodeStatusData;
import com.mdd.maduoduo.domain.interactor.UpdateTimeData;
import com.mdd.maduoduo.entity.BannerEntity;
import com.mdd.maduoduo.entity.CheckVersionEntity;
import com.mdd.maduoduo.entity.LoginEntity;
import com.mdd.maduoduo.entity.MaxConfigEntity;
import com.mdd.maduoduo.entity.MaxDataEntity;
import com.mdd.maduoduo.entity.QueryListEntity;
import com.mdd.maduoduo.entity.Response;
import com.mdd.maduoduo.entity.ShareCodeEntity;
import com.mdd.maduoduo.entity.StationEntity;
import com.mdd.maduoduo.entity.UserEntity;
import com.mdd.maduoduo.utils.ToastUtils;
import com.mdd.maduoduo.view.interfaces.AddMsgView;
import com.mdd.maduoduo.view.interfaces.AddShareCodeView;
import com.mdd.maduoduo.view.interfaces.CheckVersionView;
import com.mdd.maduoduo.view.interfaces.DeleteCodeView;
import com.mdd.maduoduo.view.interfaces.DeleteShareCodeView;
import com.mdd.maduoduo.view.interfaces.ExchangeView;
import com.mdd.maduoduo.view.interfaces.GetBannerView;
import com.mdd.maduoduo.view.interfaces.GetMaxView;
import com.mdd.maduoduo.view.interfaces.GetShareView;
import com.mdd.maduoduo.view.interfaces.GetStationView;
import com.mdd.maduoduo.view.interfaces.GetUserView;
import com.mdd.maduoduo.view.interfaces.LoginView;
import com.mdd.maduoduo.view.interfaces.LogoutView;
import com.mdd.maduoduo.view.interfaces.MaxConfigView;
import com.mdd.maduoduo.view.interfaces.QueryListView;
import com.mdd.maduoduo.view.interfaces.UpdateCodeStatusView;
import com.mdd.maduoduo.view.interfaces.UpdateTimeView;

import java.util.HashMap;

import javax.inject.Inject;

@PerActivity
public class LoginPresenter {

    @Inject
    public LoginPresenter() {
    }

    public void onDestroy() {
        loginData.dispose();
        loginView = null;
        getUserData.dispose();
        getUserView = null;
        maxConfigData.dispose();
        maxConfigView = null;
        setSwitchData.dispose();
        getMaxData.dispose();
        getMaxView = null;
        exchangeData.dispose();
        exchangeView = null;
        logoutData.dispose();
        logoutView = null;
        addShareCodeData.dispose();
        addShareCodeView = null;
        getShareCodeData.dispose();
        getShareView = null;
        deleteShareCodeData.dispose();
        deleteShareCodeView = null;
        updateTimeData.dispose();
        updateTimeView = null;
        getStationData.dispose();
        getStationView = null;
        queryListData.dispose();
        queryListView = null;
        updateCodeStatusData.dispose();;
        updateCodeStatusView = null;
        deleteCodeData.dispose();
        deleteCodeView = null;
        getBannerListData.dispose();
        getBannerView = null;
        checkVersionData.dispose();
    }

    @Inject
    LoginData loginData;

    @Inject
    GetUserData getUserData;

    @Inject
    MaxConfigData maxConfigData;

    @Inject
    SetSwitchData setSwitchData;

    @Inject
    GetMaxData getMaxData;

    @Inject
    LogoutData logoutData;

    @Inject
    ExchangeData exchangeData;

    @Inject
    AddShareCodeData addShareCodeData;

    @Inject
    GetShareCodeData getShareCodeData;

    @Inject
    DeleteShareCodeData deleteShareCodeData;

    @Inject
    UpdateTimeData updateTimeData;

    @Inject
    AddMsgData addMsgData;

    @Inject
    GetStationData getStationData;

    @Inject
    QueryListData queryListData;

    @Inject
    UpdateCodeStatusData updateCodeStatusData;

    @Inject
    DeleteCodeData deleteCodeData;

    @Inject
    GetBannerListData getBannerListData;

    @Inject
    CheckVersionData checkVersionData;

    LoginView loginView;

    GetUserView getUserView;

    MaxConfigView maxConfigView;

    GetMaxView getMaxView;

    ExchangeView exchangeView;

    LogoutView logoutView;

    AddShareCodeView addShareCodeView;

    GetShareView getShareView;

    DeleteShareCodeView deleteShareCodeView;

    UpdateTimeView updateTimeView;

    AddMsgView addMsgView;

    GetStationView getStationView;

    QueryListView queryListView;

    UpdateCodeStatusView updateCodeStatusView;

    DeleteCodeView deleteCodeView;

    GetBannerView getBannerView;

    CheckVersionView checkVersionView;

    public void setCheckVersionView(CheckVersionView checkVersionView) {
        this.checkVersionView = checkVersionView;
    }

    public void setGetBannerView(GetBannerView getBannerView) {
        this.getBannerView = getBannerView;
    }

    public void setDeleteCodeView(DeleteCodeView deleteCodeView) {
        this.deleteCodeView = deleteCodeView;
    }

    public void setUpdateCodeStatusView(UpdateCodeStatusView updateCodeStatusView) {
        this.updateCodeStatusView = updateCodeStatusView;
    }

    public void setQueryListView(QueryListView queryListView) {
        this.queryListView = queryListView;
    }

    public void setGetStationView(GetStationView getStationView) {
        this.getStationView = getStationView;
    }

    public void setAddMsgView(AddMsgView addMsgView) {
        this.addMsgView = addMsgView;
    }

    public void setUpdateTimeView(UpdateTimeView updateTimeView) {
        this.updateTimeView = updateTimeView;
    }

    public void setDeleteShareCodeView(DeleteShareCodeView deleteShareCodeView) {
        this.deleteShareCodeView = deleteShareCodeView;
    }

    public void setGetShareView(GetShareView getShareView) {
        this.getShareView = getShareView;
    }

    public void setAddShareCodeView(AddShareCodeView addShareCodeView) {
        this.addShareCodeView = addShareCodeView;
    }

    public void setLogoutView(LogoutView logoutView) {
        this.logoutView = logoutView;
    }

    public void setExchangeView(ExchangeView exchangeView) {
        this.exchangeView = exchangeView;
    }

    public void setMaxConfigView(MaxConfigView maxConfigView) {
        this.maxConfigView = maxConfigView;
    }

    public void setGetUserView(GetUserView getUserView) {
        this.getUserView = getUserView;
    }

    public void setLoginView(LoginView loginView) {
        this.loginView = loginView;
    }

    public void setGetMaxView(GetMaxView getMaxView) {
        this.getMaxView = getMaxView;
    }

    public void login(Context context,String url){
        loginData.execute(new BaseObserver<Response<LoginEntity>>() {
            @Override
            public void onNext(Response<LoginEntity> loginEntityResponse) {
                super.onNext(loginEntityResponse);
                if (loginEntityResponse.code.equals("200")) {
                    loginView.renderSuccess(loginEntityResponse);
                }else {
                    ToastUtils.showLongToast(context,loginEntityResponse.msg);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },url);
    }

    public void getUser(){
        getUserData.execute(new BaseObserver<UserEntity>() {
            @Override
            public void onNext(UserEntity userEntity) {
                super.onNext(userEntity);
                if (userEntity.code.equals("200")){
                    getUserView.renderSuccess(userEntity);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },"");
    }

    public void getMaxConfig(){
        maxConfigData.execute(new BaseObserver<MaxConfigEntity>() {
            @Override
            public void onNext(MaxConfigEntity maxConfigEntity) {
                super.onNext(maxConfigEntity);
                if (maxConfigEntity.code.equals("200")) {
                    maxConfigView.renderSuccess(maxConfigEntity);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },"");
    }

    public void setSwitch(int functionCode,int switchStatus){
        SetSwitchData.Params params = new SetSwitchData.Params();
        HashMap<String,Object> map = new HashMap<>();
        map.put("functionCode",functionCode);
        map.put("switchStatus",switchStatus);
        params.setMap(map);
        setSwitchData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },params);
    }

    public void getMaxData(){
        getMaxData.execute(new BaseObserver<MaxDataEntity>() {
            @Override
            public void onNext(MaxDataEntity maxDataEntity) {
                super.onNext(maxDataEntity);
                if (maxDataEntity.code.equals("200")){
                    getMaxView.renderSuccess(maxDataEntity);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },"");
    }

    public void exchange(Context context,String apiTid){
        ExchangeData.Params params = new ExchangeData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("apiTid",apiTid);
        params.setMap(map);
        exchangeData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                if (response.code.equals("200")){
                    exchangeView.renderSuccess(response);
                }else {
                    ToastUtils.showLongToast(context,response.msg);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },params);
    }

    public void logout(Context context){
        logoutData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                if (response.code.equals("200")) {
                    logoutView.renderSuccess(response);
                }else {
                    ToastUtils.showLongToast(context,response.msg);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, "");
    }

    public void addShareCode(Context context,String pickupSharedCode,String role){
        AddShareCodeData.Params params = new AddShareCodeData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("pickupSharedCode",pickupSharedCode);
        map.put("role",role);
        params.setMap(map);
        addShareCodeData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                if (response.code.equals("200")) {
                    addShareCodeView.renderSuccess(response);
                }else {
                    ToastUtils.showLongToast(context,response.msg);
                    if(response.code.equals("8000")){
                        context.startActivity(new Intent(context,MaxActivity.class));
                    }
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, params);
    }

    public void getShareCode(Context context){
        getShareCodeData.execute(new BaseObserver<ShareCodeEntity>() {
            @Override
            public void onNext(ShareCodeEntity shareCodeEntity) {
                super.onNext(shareCodeEntity);
                if (shareCodeEntity.code.equals("200")){
                    getShareView.renderSuccess(shareCodeEntity);
                }else {
                    ToastUtils.showLongToast(context,shareCodeEntity.msg);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, "");
    }

    public void deleteShareCode(Context context,String pickupSharedCode){
        deleteShareCodeData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                if (response.code.equals("200")){
                    deleteShareCodeView.renderSuccess(response,2);
                }else {
                    ToastUtils.showLongToast(context,response.msg);
                    if(response.code.equals("8000")){
                        // TODO: 2025/8/18 待补充跳转Max会员界面逻辑
                        context.startActivity(new Intent(context,MaxActivity.class));
                    }
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, pickupSharedCode);
    }

    public void updateTime(Context context,String scheduleTime){
        UpdateTimeData.Params params = new UpdateTimeData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("scheduleTime",scheduleTime);
        params.setMap(map);
        updateTimeData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                if (response.code.equals("200")) {
                    updateTimeView.renderSuccess(response);
                }else {
                    ToastUtils.showLongToast(context,response.msg);
                    if(response.code.equals("8000")){
                        // TODO: 2025/8/18 待补充跳转Max会员界面逻辑
                        context.startActivity(new Intent(context,MaxActivity.class));
                    }
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },params);
    }

    public void addMsg(Context context,String smsContent){
        AddMsgData.Params params = new AddMsgData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("smsContent",smsContent);
        params.setMap(map);
        addMsgData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                addMsgView.renderSuccess(response);
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },params);
    }

    public void getStation(Context context,int pickupStatus){
        getStationData.execute(new BaseObserver<StationEntity>() {
            @Override
            public void onNext(StationEntity stationEntity) {
                super.onNext(stationEntity);
                if (stationEntity.code.equals("200")) {
                    getStationView.renderSuccess(stationEntity);
                }else {
                    ToastUtils.showLongToast(context,stationEntity.msg);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, pickupStatus);

    }

    public void queryList(Context context,int pageNum,int pageSize,int pickupStatus,String stationName,int pickupTimeType){
        QueryListData.Params params = new QueryListData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("pageNum",pageNum);
        map.put("pageSize",pageSize);
        map.put("pickupStatus",pickupStatus);
        map.put("stationName",stationName);
        map.put("pickupTimeType",pickupTimeType);
        params.setMap(map);
        queryListData.execute(new BaseObserver<QueryListEntity>() {
            @Override
            public void onNext(QueryListEntity queryListEntity) {
                super.onNext(queryListEntity);
                if (queryListEntity.code.equals("200")){
                    queryListView.renderSuccess(queryListEntity);
                }else {
                    ToastUtils.showLongToast(context,queryListEntity.msg);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, params);
    }

    public void updateCodeStatus(Context context,String id,int pickupStatus,int updateType,String stationName){
        UpdateCodeStatusData.Params params = new UpdateCodeStatusData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("id",id);
        map.put("pickupStatus",pickupStatus);
        map.put("updateType",updateType);
        map.put("stationName",stationName);
        params.setMap(map);
        updateCodeStatusData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                if (response.code.equals("200")){
                    updateCodeStatusView.renderSuccess(response);
                }else {
                    ToastUtils.showLongToast(context,response.msg);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },params);
    }

    public void deleteCode(String id,int deleteType){
        DeleteCodeData.Params params = new DeleteCodeData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("id",id);
        map.put("deleteType",deleteType);
        params.setMap(map);
        deleteCodeData.execute(new BaseObserver<Response>() {
            @Override
            public void onNext(Response response) {
                super.onNext(response);
                deleteCodeView.renderDeleteSuccess(response);
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },params);
    }

    //页面编码：2001-待取件页面、2002-已取件页面、2003-取件码详情页、2004-按钮轮播图配置
    public void getBannerList(String pTag){
        getBannerListData.execute(new BaseObserver<BannerEntity>() {
            @Override
            public void onNext(BannerEntity bannerEntity) {
                super.onNext(bannerEntity);
                if (bannerEntity.code.equals("200")){
                    getBannerView.renderBannerSuccess(bannerEntity);
                }
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        },pTag);
    }

    public void checkVersion(String version){
        CheckVersionData.Params params = new CheckVersionData.Params();
        HashMap<String,Object>map = new HashMap<>();
        map.put("version",version);
        params.setMap(map);
        checkVersionData.execute(new BaseObserver<CheckVersionEntity>() {
            @Override
            public void onNext(CheckVersionEntity checkVersionEntity) {
                super.onNext(checkVersionEntity);
                checkVersionView.renderCheckSuccess(checkVersionEntity);
            }

            @Override
            public void onComplete() {
                super.onComplete();
            }

            @Override
            public void onError(int code, String message) {
                super.onError(code, message);
            }
        }, params);
    }
}
